#include <cstdio>
#include <cstring>
#include <iostream>
#include <unistd.h>
using namespace std;
int map1[1000][1000][2], map2[1000][1000][2];
int i, a, x, y, mapsize, sum = 0, times, moshi;
int c, msize, t, dituly;
void moshi1();
void moshi2();
char diname[100];
int main()
{
    char mname[100], baocun;
    printf("地图来源： 1）手动输入 2）配置文件 ");
    cin >> dituly;
    //-------------------------------------------------------------------------------------------------------------------------
    if (dituly == 1) {
	printf("输入地图大小(10-1000) :　");
	scanf("%d", &msize);
	printf("输入演算步骤数 ： ");
	cin >> t;
	while (cin >> x >> y && (x != -1 || y != -1)) {
	    printf("x,y输入-1停止输入　\n");
	    if (map1[x + 1][y + 1][0] == 0)
		map1[x + 1][y + 1][0] = 1;
	    else
		map1[x + 1][y + 1][0] = 0;

	    cout << endl;
	    cout << " ";
	    for (i = 1; i < msize + 1; i++)
		if (i < 10)
		    cout << i - 1 << " ";
		else
		    cout << i - 1;
	    cout << endl;
	    for (int i = 1; i < msize + 1; i++) {
		cout << i - 1;
		for (int j = 1; j < msize + 1; j++) {
		    if (map1[i][j][0] == 1)
			printf("O ");
		    else
			printf("- ");
		}
		printf("\n");
	    }

	    printf("\n\n");
	}

	for (int i = 0; i < 1000; i++) {
	    for (int j = 0; j < 1000; j++) {
		map2[i][j][1] = map1[i][j][1];
		map2[i][j][0] = map1[i][j][0];
	    }
	}
	/*
	for (int i = 1; i < msize + 1; i++) {
	    for (int j = 1; j < msize + 1; j++) {
		if (map1[i + 1][j + 1][0] == 1) {
		    c++;
		}
	    }
	}
	    cout << endl;
	    for (int i = 1; i < mapsize + 1; i++) {
		if (i < 10)
		    cout << i << " ";
		else
		    cout << i;
		for (int j = 1; j < mapsize + 1; j++) {
		    if (map1[i][j][0] == 1)
			printf("O ");
		    else
			printf("%c ", '-');
		    map1[i][j][1] = 0;
		}
		printf("\n");
	    }
	}
	*/
	cout << "1）查看模式 2）演示模式";
	cin >> moshi;
	if (moshi == 1) {
	    moshi1();
	}
	if (moshi == 2) {
	    moshi2();
	}
	//-------------------------SAVE------------------------------------
	printf("是否保存地图？y/n ");
	cin >> baocun;
	if (baocun == 'y') {
	    printf("输入地图名称：　");
	    scanf("%s", &mname);
	    freopen(mname, "w", stdout);
	    for (int i = 1; i < msize + 1; i++) {
		for (int j = 1; j < msize + 1; j++) {
		    if (map2[i + 1][j + 1][0] == 1) {
			c++;
		    }
		}
	    }
	    cout << msize << endl
		 << c << endl;
	    for (int i = 1; i < msize + 1; i++) {
		for (int j = 1; j < msize + 1; j++) {
		    if (map2[i + 1][j + 1][0] == 1) {
			printf(" %d %d ", i, j);
		    }
		}
	    }
	    cout << endl
		 << t << endl;
	}
	//-------------------------SAVE------------------------------------
	/*
			   printf("是否保存？y/n ");cin>>baocun;
			   if(baocun=='y'){
			   printf("输入地图名称：　");
			   scanf("%s",&mname);
			   freopen(mname,"w",stdout);
			   for(int i=1;i<msize+1;i++){
			   for(int j=1;j<msize+1;j++){
			   if(map1[i+1][j+1][0]==1){
			   c++;
			   }
			   }
			   }
			   cout<<msize<<endl<<c<<endl;
			   for(int i=1;i<msize+1;i++){
			   for(int j=1;j<msize+1;j++){
			   if(map1[i+1][j+1][0]==1){
			   printf(" %d %d ",i,j);
			   }
			   }
			   }
			   cout<<endl<<t;
			   }*/
	//-------------------------------------------------------------------

	if (dituly == 2) {
	    printf("输入地图名称");
	    cin >> diname;
	    cout << "1）查看模式 2）演示模式";
	    cin >> moshi;
	    freopen(diname, "r", stdin);

	    cin >> msize;
	    cout << msize << endl;
	    cin >> c;
	    cout << endl
		 << c << endl;
	    for (int i = 0; i < a; i++) {
		scanf("%d %d", &x, &y);
		map1[x + 1][y + 1][0] = 1;
	    }
	    cin >> t;
	    if (moshi == 1) {
		moshi1();
	    }
	    if (moshi == 2) {
		moshi2();
	    }
	}
	//-------------------------SAVE------------------------------------
	/*if(baocun=='n'){
			  freopen("temp","w",stdout);
			  for(int i=1;i<msize+1;i++){
			  for(int j=1;j<msize+1;j++){
			  if(map1[i+1][j+1][0]==1){
			  c++;
			  }
			  }
			  }
			  cout<<msize<<endl<<c<<endl;
			  for(int i=1;i<msize+1;i++){
			  for(int j=1;j<msize+1;j++){
			  if(map1[i+1][j+1][0]==1){
			  printf(" %d %d ",i,j);
			  }
			  }
			  }
			  cout<<endl<<t<<endl;
			  }*/
    }

    //-------------------------------------------------------------------
    return 0;
}

void moshi1()
{
    mapsize = msize;
    a = c;
    times = t;
    cout << "The 0" << endl;
    if (mapsize < 10)
	for (int i = 0; i < mapsize + 1; i++)
	    cout << i << " ";
    else {
	for (int i = 0; i < mapsize + 1; i++) {
	    if (i < 10)
		cout << i << " ";
	    else
		cout << i;
	}
    }
    cout << endl;
    for (int i = 1; i < mapsize + 1; i++) {
	if (i < 10)
	    cout << i << " ";
	else
	    cout << i;
	for (int j = 1; j < mapsize + 1; j++) {
	    if (map1[i][j][0] == 1)
		printf("O ");
	    else
		printf("%c ", '-');
	    map1[i][j][1] = 0;
	}
	printf("\n");
    }
    for (int w = 0; w < times; w++) {
	for (int i = 1; i < mapsize + 1; i++) {
	    for (int j = 1; j < mapsize + 1; j++) {
		if (map1[i - 1][j - 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i][j - 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i - 1][j][0] == 1)
		    map1[i][j][1]++;
		if (map1[i + 1][j - 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i - 1][j + 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i + 1][j + 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i + 1][j][0] == 1)
		    map1[i][j][1]++;
		if (map1[i][j + 1][0] == 1)
		    map1[i][j][1]++;
	    }
	}

	for (int i = 1; i < mapsize + 1; i++) {
	    for (int j = 1; j < mapsize + 1; j++) {
		if (map1[i][j][1] == 3)
		    map1[i][j][0] = 1;
		if (map1[i][j][1] > 3)
		    map1[i][j][0] = 0;
		if (map1[i][j][1] < 2)
		    map1[i][j][0] = 0;
	    }
	}
	printf("\n");
	cout << "The " << w + 1 << endl;
	if (mapsize < 10)
	    for (int i = 0; i < mapsize + 1; i++)
		cout << i << " ";
	else {
	    for (int i = 0; i < mapsize + 1; i++) {
		if (i < 10)
		    cout << i << " ";
		else
		    cout << i;
	    }
	}
	cout << endl;
	for (int i = 1; i < mapsize + 1; i++) {
	    if (i < 10)
		cout << i << " ";
	    else
		cout << i;
	    for (int j = 1; j < mapsize + 1; j++) {
		if (map1[i][j][0] == 1)
		    printf("O ");
		else
		    printf("%c ", '-');
		map1[i][j][1] = 0;
	    }
	    printf("\n");
	}
    }
}
void moshi2()
{
    mapsize = msize;
    a = c;

    times = t;
    system("clear");
    cout << "The 0" << endl;
    if (mapsize < 10)
	for (int i = 0; i < mapsize + 1; i++)
	    cout << i << " ";
    else {
	for (int i = 0; i < mapsize + 1; i++) {
	    if (i < 10)
		cout << i << " ";
	    else
		cout << i;
	}
    }
    cout << endl;
    for (int i = 1; i < mapsize + 1; i++) {
	if (i < 10)
	    cout << i << " ";
	else
	    cout << i;
	for (int j = 1; j < mapsize + 1; j++) {
	    if (map1[i][j][0] == 1)
		printf("O ");
	    else
		printf("%c ", '-');
	    map1[i][j][1] = 0;
	}
	printf("\n");
    }
    usleep(500000);
    system("clear");
    for (int w = 0; w < times; w++) {
	for (int i = 1; i < mapsize + 1; i++) {
	    for (int j = 1; j < mapsize + 1; j++) {
		if (map1[i - 1][j - 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i][j - 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i - 1][j][0] == 1)
		    map1[i][j][1]++;
		if (map1[i + 1][j - 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i - 1][j + 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i + 1][j + 1][0] == 1)
		    map1[i][j][1]++;
		if (map1[i + 1][j][0] == 1)
		    map1[i][j][1]++;
		if (map1[i][j + 1][0] == 1)
		    map1[i][j][1]++;
	    }
	}

	for (int i = 1; i < mapsize + 1; i++) {
	    for (int j = 1; j < mapsize + 1; j++) {
		if (map1[i][j][1] == 3)
		    map1[i][j][0] = 1;
		if (map1[i][j][1] > 3)
		    map1[i][j][0] = 0;
		if (map1[i][j][1] < 2)
		    map1[i][j][0] = 0;
	    }
	}
	printf("\n");
	cout << "The " << w + 1 << endl;
	if (mapsize < 10)
	    for (int i = 0; i < mapsize + 1; i++)
		cout << i << " ";
	else {
	    for (int i = 0; i < mapsize + 1; i++) {
		if (i < 10)
		    cout << i << " ";
		else
		    cout << i;
	    }
	}
	cout << endl;
	for (int i = 1; i < mapsize + 1; i++) {
	    if (i < 10)
		cout << i << " ";
	    else
		cout << i;
	    for (int j = 1; j < mapsize + 1; j++) {
		if (map1[i][j][0] == 1)
		    printf("O ");
		else
		    printf("%c ", '-');
		map1[i][j][1] = 0;
	    }
	    printf("\n");
	}
	usleep(500000);

	if (w < times - 1)
	    system("clear");
    }
}
